package com.jushu.storbox.view.mdmview;

import java.io.File;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Handler;
import android.view.View;

import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.filesystem.FileManagerFactory;
import com.jushu.storbox.filesystem.iFileManager;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;

public class ViewFactory
{
    
    private final static String TAG = "ViewFactory";
    
    private IViewCallback callback;
    
    private Context context;
    
    private Random random;
    
    public ViewFactory(Context context, String mdmPath, IViewCallback callback)
    {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.callback = callback;
        this.random = new Random();
        if (null == this.callback)
        {
            this.callback = new IViewCallback()
            {
                @Override
                public void onSuccess(View view)
                {
                }
                
                @Override
                public void onStart()
                {
                }
                
                @Override
                public void onProgress(int progress)
                {
                }
                
                @Override
                public void onFail(Exception e)
                {
                }
            };
        }
        previewerForMDMPath(mdmPath);
    }
    
    private void previewerForMDMPath(String mdmPath)
    {
        Thread thread = new Thread(new ViewRunnable(mdmPath));
        thread.start();
    }
    
    private class ViewRunnable implements Runnable
    {
        private String mdmFilePath;
        
        private String tmpFileName = "";
        
        private iFileManager mdmTools = null;
        
        public ViewRunnable(String mdmFilePath)
        {
            this.mdmFilePath = mdmFilePath;
            mdmTools = FileManagerFactory.getFileManager(context);
            tmpFileName = mdmTools.getFileName(mdmFilePath);
        }
        
        @Override
        public void run()
        {
            // TODO Auto-generated method stub
            try
            {
                initView();
            }
            catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
                callback.onFail(e);
            }
        }
        
        private void initView()
        {
            if (PublicTools.isFileType(tmpFileName, Constant.VIDEO_TYPE))
            {
                LogUtil.i(TAG, "file type is video need set screen landscape...");
                Activity activity = (Activity) context;
                if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                {
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
            callback.onStart();
            callback.onProgress(getRandomProgress(0, 10));
                LogUtil.i(TAG, "is not MDM WPS Type, need copy file to safe area..");
                
                initNONMDMView();
        }
        
        private void initNONMDMView()
        {
            LogUtil.i(TAG, "mdmPath:" + mdmFilePath);
            callback.onProgress(getRandomProgress(10, 20));
            callback.onProgress(getRandomProgress(20, 30));
            callback.onProgress(getRandomProgress(30, 50));
            callback.onProgress(getRandomProgress(50, 70));
            callback.onProgress(getRandomProgress(70, 90));
            callback.onProgress(getRandomProgress(90, 100));
            handler.sendEmptyMessage(1);
        }
                
        private Handler handler = new Handler()
        {
            public void handleMessage(android.os.Message msg)
            {
                switch (msg.what)
                {
                    case 1:
                        LogUtil.i(TAG, "handler initNONMDMView");
                        openONOMDMView();
                        break;
                    
                    default:
                        break;
                }
            };
        };
        
        private void openONOMDMView()
        {
            if (null == tmpFileName || "".equals(tmpFileName))
            {
                LogUtil.e(TAG, "[openONOMDMView]file not exist");
                Exception e = new Exception("file not exist");
                callback.onFail(e);
            }
            else
            {
                File file = new File(mdmFilePath);
                LogUtil.i(TAG,
                    "[openONOMDMView]file exists:" + file.exists() + ", path:" + file.getAbsolutePath()
                        + " | size:" + file.length());
                //
                if (file.exists() && 0 < file.length())
                {
                    View view2 = null;
                    LogUtil.i(TAG, "[openONOMDMView]file exists, path:" + file.getAbsolutePath() + " | size:"
                        + file.length());
                    //
                    if (PublicTools.isFileType(tmpFileName, Constant.DOCUMENT_TYPE))
                    {
                    	view2 = new DocumentDao(context, file.getAbsolutePath());
                    }
                    else if (PublicTools.isFileType(tmpFileName, Constant.IMAGE_TYPE))
                    {
                    // view2 = new PictureDao(context, file.getAbsolutePath());
                        view2 = new PhotoDao(context, file.getAbsolutePath());
                        // } else if (PublicTools.isFileType(tmpFileName,
                        // Constant.APK_TYPE)) {//apk
                        // view2 = new APKDao(context, file.getAbsolutePath());
                    }
                    else if (PublicTools.isFileType(tmpFileName, Constant.VIDEO_TYPE))
                    {
                        view2 = new VideoDao(context, Uri.parse(file.getAbsolutePath()));
                    }
                    else
                    {
                        view2 = null;
                    }
                    
                    //
                    callback.onProgress(getRandomProgress(80, 100));
                    if (null != view2)
                    {
                        callback.onSuccess(view2);
                    }
                    else
                    {
                        Exception e = new Exception("view not exist");
                        callback.onFail(e);
                    }
                }
                else
                {
                    LogUtil.e(TAG, "[openONOMDMView]file not exist");
                    Exception e = new Exception("file not exist");
                    callback.onFail(e);
                }
            }
            
        }
    }
    
    // callback
    public void setCallback(IViewCallback callback)
    {
        this.callback = callback;
    }
    
    private int getRandomProgress(int start, int end)
    {
        int progress = 0;
        try
        {
            if (null == random)
            {
                random = new Random();
            }
            progress = (start + random.nextInt(end - start));
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        return progress;
    }
    
}
